<template>
  <imageHtml v-if="linkData" :data="linkData"/>
</template>

<script setup name="JqtSpreadLink" lang="ts">

import { generateToken, getJqtSpreadLinkInfo } from '@/api/bus/jqtSpreadLink';
import router from '@/router';
import { useUserStore } from '@/store/modules/user';
import { isRelogin } from '@/utils/request';
import { JqtSpreadLinkVO } from '@/api/bus/jqtSpreadLink/types';
import imageHtml from '@/views/bus/spreadLink/page/imageHtml.vue'
import { getVisitorToken, setVisitorToken } from '@/utils/visitorToken';
import { saveLinksIp } from '@/api/bus/jqtSpreadLinkIp';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const route = useRoute();
const buttonLoading = ref(false);

const props = defineProps<{
  id: string;
}>();


onMounted(async () => {
  await generateTokenInfo();
  //获取链接详情
  await getDataInfo(props.id)
});

const linkData = ref<JqtSpreadLinkVO>();
const getDataInfo = async (id: string) => {
  const res = await getJqtSpreadLinkInfo(id);
  if(!res.data){
    ElMessageBox.alert('数据异常', '系统提示', {
      type: 'warning',
      confirmButtonText: '确定',
      callback: (action: any) => {
        window.close();
      },
    })
    return;
  }
  linkData.value = res.data;
};
/**
 * 生成访客码
 */
const generateTokenInfo = async () => {
  const res = await generateToken();
  //获取当前访客码和id是否一致，不一致更新访客码
  const token = getVisitorToken();
  //判断是否包含id
  if(token && token.includes(props.id)){
    //包含不需要更新访客码
  }else{
    //不存在或者不包含需要更新访客码
    setVisitorToken(res.data + '_' + props.id);
  }
  await saveLinksIpInfo();
};
/**
 * 保存访客ip
 */
const saveLinksIpInfo = async () => {
  const { UAParser } = await import('ua-parser-js');
  const parser = new UAParser();
  const result = parser.getResult();
  const param = {
    browser: result.browser.name,//浏览器
    model: result.device.model,//
    modelName: result.device.model,// 设备
    vendor: result.device.model,//设备
    pvendor: result.device.vendor,//系统
    pvendorName: result.device.vendor,//系统
    userKey: getVisitorToken(),//访客码
    spreadLinkKey: props.id,//链接key
    httpUrl: window.location.href,//链接
  }
  await saveLinksIp(param);
};
</script>

<style scoped>

</style>
